/**
 * Created by Rag_Panda on 2015/12/27.
 */
$articleController = $app.controller("AdminArticleController",function($scope){

});

//项目路由
$articleController.config(["$routeProvider",function($routeProvider){
    $routeProvider.when("/",{
        templateUrl:'/webapp/views/Admin/sArticle.html',
        controller:'sArticleController'
    });

}]);



$articleController.controller("sArticleController",function($scope,$http){
    $scope.query = {num:5,start:0,desc:true};//查询数组
    $scope.articleData= [] ;//文章信息
    $scope.articleClass = [];//文章分类信息
    $scope.flushArticle = function(){
        $scope.getArticle();
    };

    //获取所有文章
    $scope.getArticle = function(){
        $http.post("/api_sArticle",{query_limit:$scope.query}).success(function(response){
            $scope.articleData = response.data;

            $scope.nowPage = parseInt($scope.query.start/$scope.query.num +1);
            $scope.total = parseInt(response.total/$scope.query.num );
            if(response.total%$scope.query.num!=0)
            {
                $scope.total += 1;
            }
        });
    };
    //获取所有分类
    $scope.getClass = function(){
        $http.post("/api_sArticleClass",{query_limit:{}}).success(function(response){
            $scope.articleClass = response.data;
        });
    };

    //删除文章
    $scope.deleteArticle = function(articleId){
        $http.post("/api_dArticle",{article_id:articleId}).success(function(response){
            if(response.status==true)
            {
                $scope.headerMsg.msg = response.message;
                $scope.headerMsg.status = true;
                $scope.flushArticle();
            }
            else
            {
                $scope.headerMsg.msg = response.message;
                $scope.headerMsg.status = true;
                $scope.flushArticle();
            }

        });
    };
    //更改限制条件
    $scope.changeLimit = function(limit,value){
        $scope.query[limit] = value;
        $scope.flushArticle();

    };
    //倒序
    $scope.desc = function()
    {
        $scope.query.desc = !$scope.query.desc;
        $scope.flushArticle();
    };

    //上一页
    $scope.nextPage = function()
    {
        $scope.query["start"] += $scope.query["num"];
        if( $scope.query["start"] > $scope.total*$scope.query["num"] ){$scope.query["start"] = $scope.total*($scope.query["num"]-1) }
        $scope.flushArticle();
    };
    //下一页
    $scope.previousPage = function()
    {
        $scope.query["start"] -= $scope.query["num"];
        if( $scope.query["start"] < 0){$scope.query["start"] = 0 }
        $scope.flushArticle();
    };


    //标签相关函数
    //获得标签
    //$scope.labelData= [];
    //$scope.labelQuery = {};
    /*//获取标签
    $scope.getLabel = function(query_limit){
        if(query_limit = null){query_limit = $scope.labelQuery }
        $http.post("/api_sLabel",{query_limit:query_limit}).success(function(response){
            $scope.labelData= response.data;
        });
    };
    //添加新的标签
    $scope.addLabel = function(){
        $http.post("/api_aLabel",{label_name:$scope.label_name}).success(function(response){
            $scope.headerMsg.msg = response.message;
            $scope.headerMsg.status = true;

        });
    };*/
    $scope.deleteLabel = function(label_id,article_id)
    {
        $http.post("/api_removeArticleLabel",{label_id:label_id,article_id:article_id})//可能会被中间件拦截
            .success(function(response){
            $scope.headerMsg.msg = response.message;
            $scope.headerMsg.status = true;
            $scope.flushArticle();
        });

    };
    $scope.changeClass = function(articleId,classId)
    {
        if(classId == undefined){classId=null};
        $http.post("/api_uArticleClass",{article_id:articleId,class_id:classId})//可能会被中间件拦截
            .success(function(response){
                $scope.headerMsg.msg = response.message;
                $scope.headerMsg.status = true;
                $scope.flushArticle();
            });
    }


    $scope.getClass();
    $scope.flushArticle();


});
